package round82;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * User: Samuel
 * Date: 08-19-11
 * Time: 09:11 AM
 */
//round82
public class A {
    public static void main(String[] args) throws IOException {
        new A().solve();
    }

    private void solve() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String suit = in.readLine();
        String split[] = in.readLine().split(" ");
        String vals = "6789TJQKA";


        if (split[0].charAt(1) == split[1].charAt(1)) {
            if (vals.indexOf(split[0].charAt(0)) > vals.indexOf(split[1].charAt(0))) {
                System.out.println("YES");
                System.exit(0);
            } else {
                System.out.println("NO");
                System.exit(0);
            }
        }

        if (split[0].charAt(1) == suit.charAt(0)) {
            System.out.println("YES");
            System.exit(0);
        }
        System.out.println("NO");
        System.exit(0);
    }
}
